package com.wangyi.fightimg.moduls.category;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wangyi.fightimg.R;
import com.wangyi.fightimg.moduls.category.CategoryBean;

import java.util.List;

/**
 * @author wangYi
 *         图片种类列表适配器
 */

public class CategoryAdapter extends BaseAdapter {
    private List<CategoryBean> categoryBeanList;
    private Context context;

    public CategoryAdapter(Context context, List<CategoryBean> categoryBeanList) {
        this.categoryBeanList = categoryBeanList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return categoryBeanList.size();
    }

    @Override
    public Object getItem(int position) {
        return categoryBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        MyViewHolder myViewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_category, null);
            myViewHolder = new MyViewHolder(convertView);
            convertView.setTag(myViewHolder);
        } else {
            myViewHolder = (MyViewHolder) convertView.getTag();
        }
        myViewHolder.tvCategory.setText(categoryBeanList.get(position).getName());
        myViewHolder.tvCategory.setSelected(categoryBeanList.get(position).isSelected());
        if (categoryBeanList.get(position).isSelected()) {
            myViewHolder.viewLine.setVisibility(View.VISIBLE);
        } else {
            myViewHolder.viewLine.setVisibility(View.INVISIBLE);
        }
        if (myViewHolder.tvCategory.isSelected()) {
            myViewHolder.rlRoot.setBackgroundResource(R.color.white_ffffff);
        } else {
            myViewHolder.rlRoot.setBackgroundResource(R.color.gray_d1d1d1);
        }
        return convertView;
    }

    class MyViewHolder {
        RelativeLayout rlRoot;
        View viewLine;
        TextView tvCategory;

        public MyViewHolder(View view) {
            rlRoot = (RelativeLayout) view.findViewById(R.id.rl_root);
            viewLine = (View) view.findViewById(R.id.view_line);
            tvCategory = (TextView) view.findViewById(R.id.tv_category);
        }
    }
}
